package com.example.fengyungo.controller.admin;

import com.example.fengyungo.constant.MsgConstant;
import com.example.fengyungo.domain.pojo.Driver;
import com.example.fengyungo.result.Result;
import com.example.fengyungo.service.DriverService;
import com.example.fengyungo.util.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "风运Go管理端-司机管理")
@RestController
@RequestMapping("/admin/driver")
public class AdminDriverController {

    @Autowired
    private DriverService driverService;

    @Autowired
    private RedisUtil redisUtil;

    @ApiOperation("查询所有司机信息")
    @GetMapping("/allDrivers")
    @Cacheable(value = "driver", key = "'allDrivers'")
    public Result<List<Driver>> getAllDrivers() {
        List<Driver> allDrivers = driverService.getAllDrivers();
        return Result.success(allDrivers);
    }

    @ApiOperation("条件查询司机信息")
    @GetMapping("/conditions")
    public Result getDriverByCondition(Driver driver) {
        List<Driver> drivers = driverService.getDriverByCondition(driver);
        return Result.success(drivers);
    }

    /**
     * 修改司机信息
     *
     * @param driver Driver
     * @return Result
     */
    @ApiOperation("修改司机信息")
    @PutMapping("/updateDriver")
    public Result updateDriver(Driver driver) {
        Integer res = driverService.updateDriverById(driver);
        if (res > 0) {
            // 清除缓存
            redisUtil.cleanCache("fengyungodriver*");
            return Result.success();
        }
        return Result.error(MsgConstant.UPDATE_FAIL);
    }
}
